/**
 * @Author Yenchingcheng
 * @Date 2023/5/15 0:13
 * @Description 静态内部类
 **/
public class Singleton04 {

    public static void main(String[] args) {
        System.out.println("静态内部类：");
        //测试Singleton5
        Singleton5 s1 = Singleton5.getInstance();
        Singleton5 s2 = Singleton5.getInstance();
        System.out.println(s1 == s2);//返回true
    }
}

//静态内部类（兼顾效率和懒加载）
class Singleton5{
    //构造器私有化，不能new
    private Singleton5() {
    }

    //2.静态内部类，类中有一个静态属性
    private static class SingletonInstance{
        private final static Singleton5 instance = new Singleton5();
    }

    //3.提供共有的静态方法，返回实例对象
    // Singleton5装载时SingletonInstance不会立即装载，只有调用getInstance时才会装载，同时保证了线程安全
    public static synchronized Singleton5 getInstance() {
       return SingletonInstance.instance;
    }
}
